<template>
	<view class="warp">
		<view class="logo">
			<view class="info">
				<view class="info-head">
					<view class="">
						<u-avatar src="/static/images/logo/logo.jpg" size="140" :fade="true" duration="450" :lazy-load="true">
						</u-avatar>
					</view>
					<view style="font-size: 90rpx;">
						SaY Hi
					</view>
				</view>
				<view style="width: 100%;text-align: center;padding: 0 146rpx 0 160rpx;margin-top: 28rpx;">
					<u-button size="default" type="success" shape="circle" @click="userLogin">
						<u-icon name="weixin-circle-fill" size="54"></u-icon>
						<spna style="font-size: 36rpx;margin-left: 16rpx;">微信授权登陆</spna>
					</u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// import {wxLogin} from  '../../../common/utils.js'
	import {
		base
	} from '../../../utils/base.js';
	
	export default {
		data() {
			return {

			}
		},
		methods: {
			userLogin(){
				
				
				
				
				wx.getUserProfile({
					lang: 'zh_CN',
					desc: '用户登录',
					success: (res) => {
						console.log("res",res);
						const user = {
							...res.userInfo
						};
						wx.cloud.callFunction({
							name: 'getOpenid',
							success(res) {
								console.log("openid",res);
								base.ajax({
								  url: 'user/login',
								  data: {
								    ...user,
									openid:res.result.openid
								  },
								  method: 'get',
								  success: function (res) {
								    // let status = res.code;
								    // if (status == 200) {
								    //   wx.setStorageSync('uuid', res.data.user.uuid)
								    //   redis.put('token', res.data.token, 12 * 60 * 60)
								    //   if (res.data.user.universityId) {
								    //     wx.setStorageSync('universityId', res.data.user.universityId)
								    //     setTimeout(function () {
								    //       wx.redirectTo({
								    //         url: '/pages/index/index',
								    //       })
								    //     }, 800)
								    //   } else {
								    //     setTimeout(function () {
								    //       wx.redirectTo({
								    //         url: '/pages/school/searchbar/searchbar',
								    //       })
								    //     }, 800)
								    //   }
								    //   return
								    // } else if (status == 101) {
								    //   wx.hideToast({
								    //     success: (res) => {},
								    //   })
								    //   wx.showModal({
								    //     content: '由于违规操作，您的账户已被停用,如有疑问请联系侠客楼公众号',
								    //     showCancel: false,
								    //     confirmText: '确认',
								    //     success: function (res) {
								    //       // 用户没有授权成功，不需要改变 isHide 的值
								    //       if (res.confirm) {}
								    //     }
								    //   });
								    //   return
								    // } else {
								    //   wx.showToast({
								    //     title: '出错了，请稍后重试',
								    //     icon: 'none',
								    //     duration: 3000
								    //   })
								    //   return
								    // }
								  },
								  fail: function (res) {
								    // wx.showToast({
								    //   title: '网络连接失败',
								    //   icon: 'none',
								    //   duration: 3000
								    // })
								  }
								})
							}
						})
					},
				})
				
				
				return;
				
				
				
				
				
				
				
				wx.showToast({
				  title: "正在登录",
				  mask: true,
				  icon: 'loading',
				})
				wx.login({
				  success: function (res) {
				    let code = res.code; //返回code
				    wx.request({
				      url: base.baseUrl + 'wx/login',
				      data: {
				        code: code
				      },
				      header: {
				        'content-type': 'application/x-www-form-urlencoded'
				      },
				      method: 'get',
				      success: function (res) {
				        wx.setStorageSync('openid', res.data.openid)
				        if (res.statusCode == 200) {
				          wx.showToast({
				            title: "正在登录",
				            mask: true,
				            icon: 'loading',
				          })
				          base.ajax({
				            url: 'app/sys/selectSaveUser',
				            data: {
				              openId: res.data.openid,
				              loginName: e.detail.userInfo.nickName,
				              sex: e.detail.userInfo.gender,
				              headImg: e.detail.userInfo.avatarUrl,
				              // phone: phoneNumber
				            },
				            method: 'get',
				            success: function (res) {
				              let status = res.code;
				              if (status == 200) {
				                wx.setStorageSync('uuid', res.data.user.uuid)
				                redis.put('token', res.data.token, 12 * 60 * 60)
				                if (res.data.user.universityId) {
				                  wx.setStorageSync('universityId', res.data.user.universityId)
				                  setTimeout(function () {
				                    wx.redirectTo({
				                      url: '/pages/index/index',
				                    })
				                  }, 800)
				                } else {
				                  setTimeout(function () {
				                    wx.redirectTo({
				                      url: '/pages/school/searchbar/searchbar',
				                    })
				                  }, 800)
				                }
				                return
				              } else if (status == 101) {
				                wx.hideToast({
				                  success: (res) => {},
				                })
				                wx.showModal({
				                  content: '由于违规操作，您的账户已被停用,如有疑问请联系侠客楼公众号',
				                  showCancel: false,
				                  confirmText: '确认',
				                  success: function (res) {
				                    // 用户没有授权成功，不需要改变 isHide 的值
				                    if (res.confirm) {}
				                  }
				                });
				                return
				              } else {
				                wx.showToast({
				                  title: '出错了，请稍后重试',
				                  icon: 'none',
				                  duration: 3000
				                })
				                return
				              }
				            },
				            fail: function (res) {
				              wx.showToast({
				                title: '网络连接失败',
				                icon: 'none',
				                duration: 3000
				              })
				            }
				          })
				        }
								
				      }
				    })
				  }
				})
				
				
				
				
				
				
				
				
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.warp {
		background-color: #F8F8F8;
		min-height: 100vh;
	}

	.logo {
		height: 100vh;
		width: 100%;
		display: table;

		.info {
			width: 100%;
			// padding: 0 160rpx;
			display: table-cell;
			vertical-align: middle;

			.info-head {
				display: flex;
				height: 140rpx;
				line-height: 140rpx;
				justify-content: center;
			}

		}
	}
</style>
